package com.dji.custom.service;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author sean
 * @version 1.1
 * @date 2022/6/9
 */
@Slf4j
@Service
public class PythonCustomService {

    public void startPythonAi(String jobId, String liveUrl, String waylineId
                                , String deviceSn, String deviceName){
        ThreadUtil.execute(new Runnable() {
            @Override
            public void run() {
                // 调用服务 通知直播已开启
                HashMap liveMap = new HashMap<>();
                liveMap.put("jobId", jobId);
                liveMap.put("waylineId", waylineId);
                liveMap.put("deviceSn", deviceSn);
                liveMap.put("deviceName", deviceName);
                liveMap.put("liveUrl", liveUrl);
                log.info("调用远程开启直播参数. {}", liveMap);
                HttpResponse response = HttpRequest.post("http://127.0.0.1:8101/livestream_ffmpeg_start")
                        //设置请求头(可任意加)
                        .header("Content-Type", "application/json")
                        //请求参数
                        .body(JSONObject.toJSONString(liveMap))
                        .setConnectionTimeout(5000)
                        .execute();
                log.info("调用远程开启直播结果. {}", response);
            }
        });
    }

    public void stopPythonAi(String videoId, String deviceSn){
        ThreadUtil.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    // 调用服务 通知直播已开启
                    HashMap liveMap = new HashMap<>();
                    liveMap.put("deviceSn",deviceSn);
                    HttpResponse response = HttpRequest.post("http://127.0.0.1:8101/livestream_ffmpeg_end")
                            //设置请求头(可任意加)
                            .header("Content-Type", "application/json")
                            //请求参数
                            .body(JSONObject.toJSONString(liveMap))
                            .setConnectionTimeout(5000)
                            .execute();
                    log.info("调用远程关闭直播结果. {}", response);
                }catch (Exception e){
                    e.printStackTrace();
                    log.error("调用远程关闭直播异常. {}", e.getMessage());
                }
            }
        });
    }



}
